<?php

class log extends supplier {

    private $tab = array( 'login' => '登录日志', 'action' => '操作日志', 'db' => '数据库日志', 'system' => '系统日志' );

    public function index( $c, $a ) {
        $type = G( 'type', 'login' );
        $log_data = $this->get_log_data( $type );
        include $this->display( $c );
    }

    public function del() {
        if ( !$in_id = G( 'in_id' ) ) {
            exit( 'Error:001 非法删除日志!' );
        }

        if ( !$type = G( 'type' ) ) {
            exit( 'Error:002 非法删除日志!' );
        }
        $log_data = $this->get_log_data( $type );
        $id = explode( ",", $in_id );

        foreach ( $id as $k => $v ) {
            if ( isset( $log_data[ $v ] ) ) {
                unset( $log_data[ $v ] );
            }
        }

        $file = WEBROOT . 'content/log/' . md6( $type ) . '.php';
        if ( file_exists( $file ) ) {
            @unlink( $file );
        }

        foreach ( $log_data as $v ) {
            if ( $type == 'system' || $type == 'db' ) {
                $msg = array_shift( $v );
            } else {
                array_pop( $v );
                $msg = implode( '==', $v );
            }
            addlog( $type, $msg );
        }
        exit( 'success' );
    }

    private function get_log_data( $file ) {
        $path = WEBROOT . 'content/log/' . md6( $file ) . '.php';
        $data = file_get_contents( $path );
        $data = explode( "\n", $data );
        array_shift( $data );
        array_pop( $data );

        foreach ( $data as $k => $v ) {
            $tmp = explode( "==", $v );
            $log_data[ $k ] = $tmp;
        }
        unset( $data );
        return $log_data;
    }
}